<?php
/**
 *房源
 */

namespace app\badmin\controller;

use app\badmin\model\HouseType;
use app\badmin\model\Region;
use app\badmin\model\HouseErr;
use app\badmin\model\HouseTraps;
use app\common\controller\BadminController;
use app\badmin\model\HouseAuction as HouseAuctionModel;
use think\Exception;
use app\badmin\model\House as HouseModel;
use think\facade\Request;

class House extends BadminController {

    /**
     * 房源列表
     * @return false|string
     */
    public function houseList() {
        try {
            $params = $this->checkParams('type,page,long');
            if ($params === false) return $this->resErr();
            HouseModel::getList($params, $data, $page, $pageNum, $count);
            return $this->resData('success', compact('page', 'pageNum', 'count', 'data'));
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 条件数据
     * @return false|string
     */
    public function whereList() {
        try {
            $params = $this->checkParams('');
            if ($params === false) return $this->resErr();
            $area = Region::where('p_code', $params['city_code'])
                ->field('city_code,name')
                ->order('weights', 'asc')
                ->select();
            foreach ($area as $k1 => $v1) {
                $area[$k1]['key'] = $v1['city_code'];
                $area[$k1]['value'] = $v1['name'];
                unset($area[$k1]['city_code']);
                unset($area[$k1]['name']);
            }
            $a_type = HouseModel::$whereList['a_type'];
            $a_status = HouseModel::$whereList['a_status'];
            $bedroom = HouseModel::$whereList['bedroom'];
//            $bulu = HouseModel::$whereList['bulu'];
            $discount = HouseModel::allIntervel($params['city_code'])['discount_interval'];
            $house_floor = HouseModel::allIntervel($params['city_code'])['house_floor'];
            $source_type = HouseModel::$whereList['source_type'];
            $house_type = HouseType::getList();
            $other = HouseModel::$whereList['other'];
            $order = HouseModel::$whereList['order'];
            return $this->resData('success', compact('area', 'a_type', 'a_status', 'bedroom', 'discount', 'house_floor', 'source_type', 'house_type', 'other', 'order'));
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 房源详情
     * @return false|string
     */
    public function houseInfo() {
        try {
            $params = $this->checkParams('id');
            if ($params === false) return $this->resErr();
            $data = HouseModel::houseInfo($params);//房源详情
            $history = HouseAuctionModel::getHistory($params['id']);//拍卖纪录
            return $this->resData('success', compact('data', 'history'));
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 添加房源补漏信息获取
     * @return false|string
     */
    public function houseTrapsGet() {
        try {
            $data = HouseModel::$whereList['source_type'];;
            return $this->resData('success', $data);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 添加房源补漏信息提交
     * @return false|string
     */
    public function houseTrapsPost() {
        try {
            $params = $this->checkParams('source_url,type');
            if ($params === false) return $this->resErr();
            HouseTraps::add($params);
            return $this->resData('添加成功', []);
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

    /**
     * 添加纠错信息
     * @return false|string
     */
    public function houseErr() {
        try {
            if (Request::isPost()) {
                $params = $this->checkParams('house_id,content');
                if ($params === false) return $this->resErr();
                $res = HouseErr::add($params);
                if (!$res) return $this->resErr();
                else return $this->resData('添加成功', []);
            }
        } catch (Exception $e) {
            return $this->resErr($e->getMessage(), $e->getCode());
        }
    }

}
